package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ActionMap;

import unti.FullBean;

import mvc.action.LoginAction;
import mvc.action.RegisterAction;
import mvc.form.Action;
import mvc.form.ActionForm;
import mvc.form.LoginForm;

public class ActionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String sign = request.getParameter("sign");
		System.out.println("sign=" + sign);
		ActionForm form = FullBean.full(request);
		Action action = null;
		Map<String, String> map = ActionMapping.getMap();
		try {
			String actionName = map.get(sign);
			Class clazz = Class.forName(actionName);
			action = (Action) clazz.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String message = action.execute(form);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
		System.out.println(form);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
